package com.pantech.app.call_log_enabler;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.BatteryManager;
import android.util.Log;

public class SwitchReceiver extends BroadcastReceiver {
	
	private static final String LOG_TAG = "LogApp";
	public static final String ACTION_RESTART_LOGLOCATIONSERVICE = "com.pantech.app.call_log_enabler.action.RESTART_LOGLOCATIONSERVICE";
	public static final String ACTION_RESTART_ICONSERVICE = "com.pantech.app.call_log_enabler.action.RESTART_ICONSERVICE";
	public static final String ACTION_RESTART_PROCESSSERVICE = "com.pantech.app.call_log_enabler.action.ACTION_RESTART_PROCESSSERVICE";

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
			Log.e(LOG_TAG, "Make log > ACTION_BOOT_COMPLETED receved!");
			SharedPreferences.Editor ed = context.getSharedPreferences("LogAppPerference", android.content.Context.MODE_WORLD_WRITEABLE).edit();
	        ed.putBoolean("sip_IsCallLog_ongoing", false);
	        ed.commit();
	        
	        Context other = null;
			try {
				other = context.createPackageContext("com.pantech.app.vt", Context.CONTEXT_IGNORE_SECURITY);
				SharedPreferences VTPref = other.getSharedPreferences("Preferences.pref", Context.MODE_MULTI_PROCESS);
				if(VTPref.getBoolean("sip_IsCallLogEnabled", false)) {
					Intent mintent = new Intent(context, IconService.class);
			    	mintent.putExtra("text", "Saving Logs 'Call start ~ Call end'");
			    	mintent.putExtra("icon", R.drawable.emo_im_cool);
			    	context.startService(mintent);
				}
			} catch (NameNotFoundException e1) {
				Log.e(LOG_TAG, "Make log > get SharedPreference failed!!");
				e1.printStackTrace();
			}
		}else if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
			int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 2);
            
            if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB) {
            	Log.e(LOG_TAG, "Make log > USB connected!");
            	context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + (new File(String.format("" + MainActivity.Folder_path + MainActivity.Log_path_VT))))));
            	context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + (new File(String.format("" + MainActivity.Folder_path + MainActivity.Log_path))))));
            }
        }else if(action.equals(ACTION_RESTART_LOGLOCATIONSERVICE)) {
        	Log.e(LOG_TAG, "Make log > restart LogLocationService!!");
    		
    		Intent mintent = new Intent(context, LogLocationService.class);
        	mintent.putExtra("gps", intent.getBooleanExtra("gps", false));
        	mintent.putExtra("network", intent.getBooleanExtra("network", false));
        	mintent.putExtra("refreshingTime", intent.getIntExtra("refreshingTime", 0));
        	context.startService(mintent);
        }else if(action.equals(ACTION_RESTART_ICONSERVICE)) {
        	Log.e(LOG_TAG, "Make log > restart IconService!!");
    		
        	Intent mintent = new Intent(context, IconService.class);
        	mintent.putExtra("text", intent.getStringExtra("text"));
        	mintent.putExtra("icon", intent.getIntExtra("icon", R.drawable.emo_im_laughing));
        	context.startService(mintent);
        }else if(action.equals(ACTION_RESTART_PROCESSSERVICE)) {
        	Log.e(LOG_TAG, "Make log > restart ProcessService!!");
    		
        	Intent mintent = new Intent(context, ProcessService.class);
        	context.startService(mintent);
        }
	}
}
